package com.qding.park.manager.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.annotation.log.OperationLogType;
import com.qding.park.common.core.annotation.log.SystemControllerLog;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.auth.AuthException;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.common.web.controller.BaseController;
import com.qding.park.manager.cache.ParkCacheInter;
import com.qding.park.manager.cache.SystemCacheInter;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.ConfDateFacade;
import com.qding.park.manager.pojo.SOAServiceEnum;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.web.local.cache.DicCache;
import com.qding.park.manager.web.power.Power;
import com.qding.smart.sso.client.entry.UserInfo;
import com.qding.smart.sso.client.util.SSOClientHelp;

@RestController
@RequestMapping("/system")
public class Web_SystemController extends BaseController {
    private static final Logger _log = LoggerFactory.getLogger(Web_SystemController.class);


	@Autowired
	private SystemCacheInter systemCacheInter;
	
//	@Autowired
//	private DicCache  dicCache;
	
	@Autowired
	private  ConfDateFacade   confDateFacade;

	@SystemControllerLog(description = "清除所有缓存", operation = OperationLogType.system_clearCache)
	@RequestMapping("/clearCache")
	public AnswerMsg clearCache() {
		UserInfo user = SSOClientHelp.getUserInfo(request);
		if (!user.isAdaminIs()) {
			// 如果不是管理员
			throw new AuthException(AuthException.TypeEnum.EXCEPTION_AUTH.getId(), null,
					ExceptionMessage.createExceptionMessage(AuthException.TypeEnum.EXCEPTION_AUTH.getDesc(),
							user.getUserName() + " 没有此权限操作", "权限不足:"));
		}
		// 删除所有的缓存数据
		systemCacheInter.clearBaseCache();
		return AnswerMsgFactoryImpl.createSuccessAnserMsg("清除缓存!!");
	}

	@Power(visitPark = "#parkId")
	@SystemControllerLog(description = "车场数据同步", operation = OperationLogType.system_confSync)
	@RequestMapping("/confSync")
	public AnswerMsg confSync(String parkId) {

		/****** 验证字段 ********/
		if (!StringUtils.isBlank(parkId)) {
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("parkId", parkId);

			vb.getIntValidate().put("parkId", parkId);
			RchlValidateTools.validate(vb);
		}
		/**************/
		
		
//		/*根据厂商编号 获取对应的服务层对象*/
//		String supplierId=dicCache.querysupplierIdByid(parkId);
//		ConfDateFacade confDataFacade= this.getServiceObj(SOAServiceEnum.BASEDATE_SYNC_SERVICE_NAME, supplierId, ConfDateFacade.class);
//		/*根据厂商编号 获取对应的服务层对象*/
		
		
		if (!StringUtils.isBlank(parkId)) {
			int parkIdInt = Integer.valueOf(parkId);
			confDateFacade.confSync(parkIdInt);
		} else {
		    confDateFacade.confSync();
		}

		AnswerMsg answerMsg = AnswerMsgFactoryImpl.createSuccessAnserMsg("同步完成");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("syncTime", (new Date()).getTime() / 1000);
		answerMsg.setData(data);
		return answerMsg;
	}
}
